package cn.liyicun.cp.cpweb.service;

import cn.hutool.core.util.StrUtil;
import cn.liyicun.cp.cpweb.entity.*;
import cn.liyicun.cp.cpweb.mapper.UserInfoMapper;
import cn.liyicun.cp.cpweb.mapper.UserLoginLogMapper;
import cn.liyicun.cp.cpweb.util.MyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserInfoService extends BaseService {

	@Autowired
	private UserInfoMapper userInfoMapper;

	@Autowired
	private UserMoneyService userMoneyService;
@Autowired
	UserLoginLogMapper userLoginLogMapper;

	public PageInfo<UserInfo> search(Long uid,String userName, int currentPage, int pageSize) {
		UserInfoExample example = new UserInfoExample();
		UserInfoExample.Criteria cri = example.createCriteria();
		if (uid != null && uid > 0) {
			cri.andIdEqualTo(uid);
		}
		if(StrUtil.isNotBlank(userName)){
			cri.andUserNameLike("%"+userName+"%");
		}
		example.setOrderByClause("id desc");
		PageHelper.startPage(currentPage, pageSize);
		List<UserInfo> docs = userInfoMapper.selectByExample(example);
		PageInfo<UserInfo> pageInfo = new PageInfo<>(docs);
		return pageInfo;
	}


	public void create(String userName, String passwd, String phone, String qq, Long pid) {
		Date now = new Date();
		UserInfo record = new UserInfo();
		record.setPhone(phone);
		record.setUserName(userName.toLowerCase().trim());
		record.setCreateTime(now);
		record.setQq(qq);
		record.setStatus(1);
		record.setPid(pid);
		record.setPasswd("");
		record.setUpdateTime(now);
		if (uniqueUserName(userName) > 0) {
			error("账号重复" + userName);
		}
		userInfoMapper.insertSelective(record);
		Long id = record.getId();
		updatePasswd(id, passwd);
		userMoneyService.initMoney(record.getId());
	}

	// 忘记密码,后台强制修改
	public void updatePasswd(Long id, String passwd) {
		UserInfo record = new UserInfo();
		record.setId(id);
		record.setPasswd(MyUtil.passwd(passwd, id));
		record.setUpdateTime(new Date());
		userInfoMapper.updateByPrimaryKeySelective(record);
	}

	// 修改用户状态
	public void updateStatus(Long id, Integer status) {
		UserInfo record = new UserInfo();
		record.setId(id);
		record.setStatus(status);
		record.setUpdateTime(new Date());
		userInfoMapper.updateByPrimaryKeySelective(record);
	}

	// 修改密码
	public void changePasswd(Long id, String oldPasswd, String newPasswd) {
		UserInfo info = userInfoMapper.selectByPrimaryKey(id);
		String passwd = info.getPasswd();
		compare(!passwd.equals(MyUtil.passwd(oldPasswd, id)), "旧密码错误!");

		UserInfo record = new UserInfo();
		record.setId(id);
		record.setPasswd(MyUtil.passwd(newPasswd, id));
		record.setUpdateTime(new Date());
		userInfoMapper.updateByPrimaryKeySelective(record);
	}

	public void userUpdateBank(Long id, String bankAddr, String bankCard, String bankName, String bankUser){
		UserInfo info = userInfoMapper.selectByPrimaryKey(id);
		if(StrUtil.isNotBlank(info.getBankUser())) {
			bankUser=info.getBankUser();
		}
		updateBank(id, bankAddr, bankCard, bankName, bankUser);
	}

	public void updateBank(Long id, String bankAddr, String bankCard, String bankName, String bankUser) {
		UserInfo record = new UserInfo();
		record.setBankAddr(bankAddr);
		record.setBankCard(bankCard);
		record.setId(id);
		record.setBankName(bankName);
		record.setBankUser(bankUser);
		record.setUpdateTime(new Date());
		userInfoMapper.updateByPrimaryKeySelective(record);
	}

	public long uniqueUserName(String userName) {
		UserInfoExample example = new UserInfoExample();
		example.createCriteria().andUserNameEqualTo(userName);
		return userInfoMapper.countByExample(example);
	}

	public UserInfo getUserInfoByUserName(String userName) {
		UserInfoExample example = new UserInfoExample();
		example.createCriteria().andUserNameEqualTo(userName);
		List<UserInfo> list = userInfoMapper.selectByExample(example);

		for (UserInfo userInfo : list) {
			return userInfo;
		}
		return null;
	}

	public LoginInfo login(String userName, String passwd, String captcha,String ip) {
		UserInfo info = getUserInfoByUserName(userName);
		compare(info == null, "用户名或是密码错误!!");
		Long uid = info.getId();
		String passwd2 = MyUtil.passwd(passwd, uid);
		compare(!passwd2.equals(info.getPasswd()), "用户名或是密码错误!");
		compare(info.getStatus() != 1, "该用户已经禁用!");


		LoginInfo login = new LoginInfo();
		login.setUid(uid);
		login.setUserName(userName);
		login.setMoney(userMoneyService.getMoneyInfo(uid).getMoney());

		UserLoginLog rec=new UserLoginLog();
		rec.setCreateTime(new Date());
		rec.setUid(uid);
		rec.setCreateIp(ip);
		userLoginLogMapper.insertSelective(rec);
		return login;
	}


	public  UserInfo getUserInfo(Long id){
		return userInfoMapper.selectByPrimaryKey(id);
	}

}
